package com.liugh.service.zhongche.Imp;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.liugh.entity.standard.StandardAdvise;
import com.liugh.mapper.zhongche.StandardAdviseMapper;
import com.liugh.model.DownloadModel;
import com.liugh.service.zhongche.IStandardAdviseService;
import com.liugh.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Fatao on 2018/11/12 .
 */

@Service
public class StandardAdviseServiceImpl implements IStandardAdviseService {


    @Autowired
    private StandardAdviseMapper standardAdviseMapper;


    /**
     * 标准征求意见添加 .
     * @param standardAdvise .
     * @return .
     */
    public StandardAdvise addStandAdvise(StandardAdvise standardAdvise) {
        return CommonUtil.isDBOpSuccess(standardAdviseMapper.insert(standardAdvise)) ? standardAdvise : null;
    }


    /**
     * 更新标准征求意见 .
     * @param standardAdvise .
     * @return .
     */
    public StandardAdvise updateStandAdvise(StandardAdvise standardAdvise) {
        return CommonUtil.isDBOpSuccess(standardAdviseMapper.updateById(standardAdvise)) ? standardAdvise : null;
    }


    /**
     * 根据id删除一条征求意见 .
     * @param id .
     * @return .
     */
    public int deleteStandAdvise(int id) {
        return standardAdviseMapper.deleteById(id);
    }


    /**
     * 根据id获取一条征求意见 .
     * @param id .
     * @return .
     */
    public StandardAdvise getAdviseById(int id) {
        return standardAdviseMapper.selectById(id);
    }


    /**
     * 分页获取所有的征求意见.
     * @param page .
     * @return .
     */
    public Page<StandardAdvise> getAllStandAdvise(Page<StandardAdvise> page) {
        return page.setRecords(standardAdviseMapper.selectPage(page, new EntityWrapper<StandardAdvise>().orderBy("create_time", false)));
    }

    @Override
    public List<DownloadModel> downAttache(int id) {
        List<DownloadModel> downloadModels = new ArrayList<>(2);
        StandardAdvise standardAdvise = getAdviseById(id);

        //        返回空表格的下载地址及文件名
        if (!StrUtil.isEmpty(standardAdvise.getAdviseDir())) {
             String name = CommonUtil.getFileNameByUrl(standardAdvise.getAdviseDir());
            if (!StrUtil.isEmpty(name)) {
               DownloadModel downloadModel = new DownloadModel(standardAdvise.getAdviseDir(),name);
               downloadModels.add(downloadModel);
            }
        }

        //        返回填满意见的表格的下载地址及文件名
        if (!StrUtil.isEmpty(standardAdvise.getFullAdviseDir())) {
            String name = CommonUtil.getFileNameByUrl(standardAdvise.getFullAdviseDir());
            if (!StrUtil.isEmpty(name)) {
                DownloadModel downloadModel = new DownloadModel(standardAdvise.getFullAdviseDir(),name);
                downloadModels.add(downloadModel);
            }
        }
        return downloadModels;
    }


}
